import loginView from "$v/loginView.ejs"
import {
    loginRequest
} from "$a/admin.js"

export default ((req, res) => {
    res.render(loginView())

    // 点击登录逻辑

    // 获取登录按钮
    const oLogin = document.getElementById("adminLogin")
    //获取账号输入框
    const OCount = document.getElementById("adminCount")
    //获取密码输入框
    const oPassword = document.getElementById("adminPassword")
 
    oLogin.onclick = async function (e) {
        e.preventDefault()
        // 获取输入的账号
        const adminNameValue = OCount.value
        // 获取输入的密码
        const passwordValue = oPassword.value

        // console.log(adminNameValue, passwordValue);

        // 登录请求
        try {
            const loginResult = await loginRequest({
                adminName: adminNameValue,
                password: passwordValue,
            })
            // console.log(loginResult);//打印出来的token
            // 登陆成功可以拿到token,把token保存在localStorage中，并跳转到首页
            localStorage.setItem("user_token", loginResult.userToken)
            router.go("/adv/adminList")
        } catch (e) {
            alert("登陆异常" + e.message)
        }


    }

})